<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class StoreGoodsCategory extends Model
{
    /**
     * 关联的数据表
     */
    protected $table = 'ims_yz_store_goods_category';

    /**
     * 可以批量赋值的属性
     */
    protected $fillable = [
        'category_id',
        'store_id',
        'created_at',
        'updated_at',
        'deleted_at'
    ];

    /**
     * 自动转换的属性
     */
    protected $casts = [
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime'
    ];

    /**
     * 关联商家
     */
    public function store()
    {
        return $this->belongsTo(Store::class, 'store_id', 'id');
    }

    /**
     * 关联商品分类
     */
    public function category()
    {
        return $this->belongsTo(GoodsCategory::class, 'category_id', 'id');
    }

    /**
     * 根据商家ID查询分类关联
     */
    public static function findByStoreId($storeId)
    {
        return static::where('store_id', $storeId)->get();
    }

    /**
     * 根据商家ID获取分类ID列表
     */
    public static function getCategoryIdsByStoreId($storeId)
    {
        return static::where('store_id', $storeId)
            ->pluck('category_id')
            ->toArray();
    }

    /**
     * 根据分类ID查询商家关联
     */
    public static function findByCategoryId($categoryId)
    {
        return static::where('category_id', $categoryId)->get();
    }
}
